import random

#地址类
class address:
    def __init__(self, country, province, street, h_number):
        self.country = country
        self.province = province
        self.street = street
        self.h_number = h_number

    def print_address(self):
        print(f"国家:{self.country},省份:{self.province},街道:{self.street},门牌号:{self.h_number}")

#用户类
class user:
    def __init__(self,kind,u_name,password,address,balance,bank_name):
        self.u_number = ''.join(str(random.randint(0,9)) for _ in range(8))
        self.kind = kind
        self.u_name = u_name
        self.password = password
        self.address = address
        self.bank_name = bank_name
        self.balance = balance

    def add_money(self,balance):
        self.balance += balance

    def take_money(self,balance):
        if self.balance < balance:
            print("余额不足")
            return 3
        self.balance -= balance

    def print_user_info(self):
        print(f"用户编号: {self.u_number}")
        print(f"用户类型: {self.kind}")
        print(f"密码:{self.password}")
        print(f"用户姓名: {self.u_name}")
        print("用户地址信息:",end="")
        self.address.print_address()
        print(f"账户余额: {self.balance}")
        print(f"所属银行: {self.bank_name}")

class o_user:
    def __init__(self,u_number,kind,u_name,password,address,balance,bank_name):
        self.u_number = u_number
        self.kind = kind
        self.u_name = u_name
        self.password = password
        self.address = address
        self.bank_name = bank_name
        self.balance = balance

    def add_money(self,balance):
        self.balance += balance

    def take_money(self,balance):
        if self.balance < balance:
            print("余额不足")
            return 3
        self.balance -= balance

    def print_user_info(self):
        print(f"用户编号: {self.u_number}")
        print(f"用户类型: {self.kind}")
        print(f"密码:{self.password}")
        print(f"用户姓名: {self.u_name}")
        print("用户地址信息:",end="")
        self.address.print_address()
        print(f"账户余额: {self.balance}")
        print(f"所属银行: {self.bank_name}")

#银行类
class bank:
    def __init__(self):
        self.users = {}
        self.bank_name = "中国农业银行的昌平沙河支行"

    def add_user(self,user):
        if len(self.users)<100:
            self.users[user.u_number] = user
            print(f"用户 {user.u_name} 添加成功，账号为 {user.u_number}")
            return 1
        else:
            print("银行用户库已满，无法添加新用户")
            return 3
    def print_user(self):
        print(self.users)
bank = bank()

#param 假数据
address1 = address("中国","河南","迎春街","003")
address2 = address("中国","河北","顺达路","001")

user1 = user(1,"张三","123456",address1,60000,bank_name=bank.bank_name)
user2 = user(2,"李四","123456",address2,60000,bank_name=bank.bank_name)
user3 = o_user("19470793",1,"王五","123456",address1,60000,bank_name=bank.bank_name)
user4 = o_user("11861515",1,"赵五","123456",address1,60000,bank_name=bank.bank_name)

bank.add_user(user1)
bank.add_user(user2)
bank.add_user(user3)
bank.add_user(user4)
for i in bank.users:
    bank.users[i].print_user_info()
    print("-----------------------")

print("*" * 40)
# 打印系统名称
print("*" + " " * 8 + "中国农业银行账户管理系统" + " " * 8 + "*")
# 打印分隔装饰线
print("*" * 40)
# 打印选项标题
print("*" + " " * 17 + "选项" + " " * 17 + "*")
# 打印各个选项
print("*" + " " * 16 + "1.开户" + " " * 16 + "*")
print("*" + " " * 16 + "2.存钱" + " " * 16 + "*")
print("*" + " " * 16 + "3.取钱" + " " * 16 + "*")
print("*" + " " * 16 + "4.转账" + " " * 16 + "*")
print("*" + " " * 16 + "5.查询" + " " * 16 + "*")
print("*" + " " * 16 + "6.Bye" + " " * 17 + "*")
# 打印底部装饰线
print("*" * 40)

#param 判断账号是否存在
def check_account(u_number):
    try:
        #账户已存在
        if bank.users[u_number] is not None:
            return 2;
        #账户不存在
    except KeyError:
        print("该账户不存在")
        return 1
#param 判断密码是否正确
def check_password(u_number,password):
    if bank.users[u_number].password == password:
        #正确返回1
        return 1
    else:
        #错误返回2
        return 2

#param 开户
def open_account(user):
    flag = check_account(user)
    if flag == 2:
        print("开户：该账户已存在")
        return 2
    elif flag == 1:
        bank.add_user(user)

#param 存钱
def save_money(u_number,money_count):
    flag = check_account(u_number)
    if flag == 2:
        print("账户存在")
        bank.users[u_number].add_money(money_count)
        print(f"存钱成功，当前余额{bank.users[u_number].balance}")
    else:
        print("账户不存在，无法存钱")
        return False

#param 取钱
def take_money(u_number,password,money_count):
    flag = check_account(u_number)
    if flag == 2:
        print("账户存在")
        if check_password(u_number,password) == 1:
            print("取出金额:",money_count)
            bank.users[u_number].take_money(money_count)
            print("剩余金额:",bank.users[u_number].balance)
        elif check_password(u_number,password) == 2:
            print("密码错误")
            return 2
    else:
        print("账户不存在，无法存钱")
        return 1

#param 转账
def transfer_money(my_account,other_account,my_password,transfer_count):
    my_flag = check_account(my_account)
    other_flag = check_account(other_account)

    my_user = bank.users[my_account]

    if my_flag == 2 and other_flag == 2:
        print("转入，转出账号都存在")
        pass_flag = check_password(my_account,my_password)
        if pass_flag == 1:
            if my_user.kind == 1 and transfer_count <= 50000:
                if my_user.balance > transfer_count:
                    print("金卡用户，可以转出")
                    transfer_flag = bank.users[my_account].take_money(transfer_count)
                    if transfer_flag == 3:
                        print("转账失败，余额不足")
                    else:
                        bank.users[other_account].add_money(transfer_count)
                        print("转账成功")

            elif my_user.kind == 2 and transfer_count <= 20000:
                if my_user.balance > transfer_count:
                    print("金卡用户，可以转出")
                    transfer_flag = bank.users[my_account].take_money(transfer_count)
                    if transfer_flag == 3:
                        print("转账失败，余额不足")
                    else:
                        bank.users[other_account].add_money(transfer_count)
                        print("转账成功")
            else:
                print("转账失败，您的账户类型不支持大笔转账")
        else:
            return 2
    else:
        return 1

#param 查询
def query_account(u_number,passwrod):
    flag = check_account(u_number)
    if flag == 2:
        print("查询：该用户存在")
        if check_password(u_number,passwrod) == 1:
            print(bank.users[u_number].print_user_info())
        else:
            print("密码错误")
    else:
        print("查询：该用户不存在")


choose = int(input("请输入所需办理业务的数字:"))

#TODO 实现逻辑
if choose == 1:
    kind = input("请输入账户类型(1为金卡，2为普通):"),
    name = input("请输入姓名："),
    flag = True
    while flag:
        password = input("请输入6位密码:"),
        str_passwrod = str(password[0])
        if len(str_passwrod) != 6:
            print("请输入六位数密码")
            continue
        elif len(str_passwrod) == 6:
            break

    address_n = address(input("请输入国家:"),
                      input("请输入省份:"),
                      input("请输入街道:"),
                      input("请输入门牌号:")
                    ),

    user = user(
                kind,name,password,address_n,
                balance=0,
                bank_name = bank.bank_name
                )
    open_account(user)
elif choose == 2:
    # 测试
    # save_money('19470793',10000)
    save_money(input("请输入您的账户:"),int(input("请输入存入金额:")))
elif choose == 3:
    # 测试
    # take_money('19470793','123456',10000)
    account = input("请输入您的账户:")
    password = input("请输入您的密码:")
    money = int(input("请输入您的转账金额"))

elif choose == 4:
    # 测试
    # account = '19470793'
    # password = '123456'
    # transfer_money('19470793','11861515','123456',60000)


    account = input("请输入您的账户:")
    check_account(account)
    password = input("请输入您的密码:")
    check_password(account,password)
    o_account = input("请输入转入账户:")
    check_account(o_account)
    money = int(input("请输入您要转账的金额"))
    transfer_money(account,o_account,password,money)

elif choose == 5:
    query_account('19470793','123456')
elif choose == 6:
    print("再见，期待您的光临")